package fi.iki.joker.mobilogger;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

/**
 * This class, along with the ListThisAndThat classes, should probably be replaced with a Tab implementation. Now the "tabs" are just buttons on top
 * of the layout which switch between the underlying listView implementation. At the time of this writing the Android SDK didn't have 
 * documented support for Tabs so I made it like this.
 * @author juhak
 *
 */
public class ButtonBar {
	
	/* identify the buttons, can be anything */
	
    public static final int BUTTON_MEMO = 0xF001;
    public static final int BUTTON_BLOG_ENTRIES = 0xF002;
    public static final int BUTTON_BLOGS = 0xF003;

    private static Button memoButton = null;
    private static Button blogEntryButton = null;
    private static Button blogsButton = null;
    
    public static Activity parent;

    public static void createButtons(Activity caller){
        parent = caller;
        memoButton = (Button) caller.findViewById(BUTTON_MEMO);
        memoButton.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View arg0) {
                    Intent i = new Intent();
                    i.setClass(ButtonBar.parent, ListMemos.class);
                    ButtonBar.parent.startActivity(i);
                    ButtonBar.parent.finish();
                }
        	});
        blogEntryButton = (Button) caller.findViewById(BUTTON_BLOG_ENTRIES);
        blogEntryButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                Intent i = new Intent();
                i.setClass(ButtonBar.parent, ListBlogEntries.class);
                ButtonBar.parent.startActivity(i);
                ButtonBar.parent.finish();
            }
        });
        blogsButton = (Button) caller.findViewById(BUTTON_BLOGS);
        blogsButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                Intent i = new Intent();
                i.setClass(ButtonBar.parent, ListBlogConfigs.class);
                ButtonBar.parent.startActivity(i);
                ButtonBar.parent.finish();
            }
        });
    }   
    
    public static void requestFocusForButton(int button) {
    	if(button == BUTTON_BLOG_ENTRIES) {
    		blogEntryButton.requestFocus();
    	} else if(button == BUTTON_BLOGS) {
    		blogsButton.requestFocus();
    	} else if(button == BUTTON_MEMO) {
    		memoButton.requestFocus();
    	}
    }
    
    public static class ButtonContainer extends TableLayout {

        public ButtonContainer(Context context, AttributeSet attrs) {

            super(context, attrs);

            TableRow row = new TableRow(context);
            // One button for each of the functionalities
            Button memoButton = new Button(context);
            Button blogEntryButton = new Button(context);
            Button blogConfigButton = new Button(context);
            memoButton.setText("Memos");
            blogEntryButton.setText("Blog entries");
            blogConfigButton.setText("Configurations");
            memoButton.setId(BUTTON_MEMO);
            blogEntryButton.setId(BUTTON_BLOG_ENTRIES);
            blogConfigButton.setId(BUTTON_BLOGS);
            
            row.addView(memoButton, new TableRow.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            row.addView(blogEntryButton, new TableRow.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            row.addView(blogConfigButton, new TableRow.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            setColumnStretchable(0, true);
            setColumnStretchable(1, true);
            setColumnStretchable(2, true);
            
            addView(row, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        }
    }       
}
